#!/bin/sh
### BEGIN INIT INFO
# Provides:          delayed_job
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Manage delayed_job worker
# Description:       Start, stop, restart delayed_job workers for a specific application.
### END INIT INFO
set -e

# Feel free to change any of the following variables for your app:
CMD="cd <%= current_path %>; RAILS_ENV=<%= fetch(:rails_env) %> <%= fetch(:mb_delayed_job_script) %>"
AS_USER=<%= fetch(:mb_delayed_job_user, user) %>
set -u

run () {
  if [ "$(id -un)" = "$AS_USER" ]; then
    eval $1
  else
    su -c "$1" - $AS_USER
  fi
}

case "$1" in
start)
  run "$CMD start <%= fetch(:mb_delayed_job_args) %>"
  ;;
stop)
  run "$CMD stop"
  ;;
restart)
  run "$CMD restart <%= fetch(:mb_delayed_job_args) %>"
  ;;
esac
